package com.ld.zxw.service;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;

import com.ld.zxw.config.Config;
import com.ld.zxw.config.ConfigIndex;
import com.ld.zxw.config.IndexFactory;
import com.ld.zxw.config.LuceneDataSource;
import com.ld.zxw.index.AddIndex;
import com.ld.zxw.index.DeleteIndex;
import com.ld.zxw.index.QueryIndex;
import com.ld.zxw.index.UpdateIndex;
import com.ld.zxw.page.Page;
import com.ld.zxw.util.CommonUtil;
import com.ld.zxw.util.DateUtil;
@SuppressWarnings("all")
public class LuceneServiceImpl implements LuceneService {
	
	private static Config config = null;
	
	private Logger log = Logger.getLogger(LuceneServiceImpl.class);
	
	public LuceneServiceImpl(String sourceKey){
		config = LuceneDataSource.getConfig(sourceKey);
	}

	@Override
	public <T> boolean saveObjs(List<T> objs) {
		long time = DateUtil.getTime();
		IndexFactory create = ConfigIndex.create(AddIndex :: new);
		boolean flag = create.saveIndexs(config, CommonUtil.turnDoc(objs, config));
		DateUtil.timeConsuming("添加索引", time);
		return flag;
	}
	@Override
	public boolean saveDocuments(List<Document> documents) {
		long time = DateUtil.getTime();
		IndexFactory create = ConfigIndex.create(AddIndex :: new);
		boolean flag = create.saveIndexs(config,documents);
		DateUtil.timeConsuming("添加索引", time);
		return flag;
	}

	@Override
	public boolean delAll() {
		long time = DateUtil.getTime();
		IndexFactory create = ConfigIndex.create(DeleteIndex :: new);
		boolean flag = create.deleteAll(config);
		DateUtil.timeConsuming("删除索引", time);
		return flag;
	}

	@Override
	public boolean delKey(Term term) {
		long time = DateUtil.getTime();
		IndexFactory create = ConfigIndex.create(DeleteIndex :: new);
		boolean flag = create.deletekey(config,term);
		DateUtil.timeConsuming("删除索引", time);
		return flag;
	}
	@Override
	public boolean delKey(Query query) {
		long time = DateUtil.getTime();
		IndexFactory create = ConfigIndex.create(DeleteIndex :: new);
		boolean flag = create.deletekey(config,query);
		DateUtil.timeConsuming("删除索引", time);
		return flag;
	}

	@Override
	public <T> boolean updateObjs(List<T> objs,Term term) {
		long time = DateUtil.getTime();
		IndexFactory create = ConfigIndex.create(UpdateIndex :: new);
		boolean flag = create.updateIndexs(config, CommonUtil.turnDoc(objs, config),term);
		DateUtil.timeConsuming("更新索引", time);
		return flag;
	}

	@Override
	public <T> List<T> findList(Query query,Class<T> obj,int num,Sort sort) {
		long time = DateUtil.getTime();
		IndexFactory create = ConfigIndex.create(QueryIndex :: new);
		List<T> findList = create.findList(config,query, obj,num,sort);
		DateUtil.timeConsuming("查询索引", time);
		return findList;
	}

	@Override
	public <T> Page<T> findList(Query query, int pageNumber, int pageSize, Class<T> obj,Sort sort) {
		long time = DateUtil.getTime();
		IndexFactory create = ConfigIndex.create(QueryIndex :: new);
		Page<T> findPageList = create.findList(config, query, pageNumber, pageSize, obj,sort);
		DateUtil.timeConsuming("查询索引", time);
		return findPageList;
	}


	@Override
	public boolean updateDocuments(List<Document> objs,Term term) {
		long time = DateUtil.getTime();
		IndexFactory create = ConfigIndex.create(UpdateIndex :: new);
		boolean flag = create.updateIndexs(config,objs,term);
		DateUtil.timeConsuming("更新索引---》", time);
		return flag;
	}

	
}
